package com.wtwd.campus.receiver;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.campus.annotation.MsgType;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.entity.*;
import com.wtwd.campus.h5_manager.entity.DeviceLog;
import com.wtwd.campus.h5_manager.service.DeviceLogService;
import com.wtwd.campus.service.DeviceInfoService;
import com.wtwd.campus.service.IRMFeignClientService;
import com.wtwd.campus.service.LogService;
import com.wtwd.campus.utils.PushUtils;
import com.wtwd.campus.utils.ReceiverUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@MsgType(value = Constants.CALL_RECORDS)
public class CallRecordsReceiverHandler implements IReceiverHandler {
    @Autowired
    private DeviceInfoService deviceInfoService;
    @Autowired
    private ReceiverUtils receiverUtils;
    @Autowired
    private LogService logService;
    @Autowired
    private IRMFeignClientService irmFeignClientService;
    @Autowired
    private PushUtils pushUtils;
    @Autowired
    private DeviceLogService deviceLogService;

    @Override
    public void handlerMessage(MqMessage mqMessage) {
        MqCallNoteMessage mqCallNoteMessage = (MqCallNoteMessage) mqMessage.getObject();
        String imei = mqCallNoteMessage.getImei();
        String commandNumber = mqCallNoteMessage.getCommandNumber();
        Integer callType = mqCallNoteMessage.getCallType();
        String duration = mqCallNoteMessage.getDuration();
        String phone = mqCallNoteMessage.getPhone();
        String timeResult = dealTime(duration);
        String deviceDate = mqCallNoteMessage.getDeviceDate();
        String deviceTime = mqCallNoteMessage.getDeviceTime();
        Long time = getTime(deviceDate, deviceTime);
        DeviceInfo deviceInfo = deviceInfoService.getDeviceByImei(imei);
        if (deviceInfo != null) {
            MqLocationData mqLocationData = mqCallNoteMessage.getMqLocationData();
            if (mqLocationData.getLatitude() != 0 && mqLocationData.getLongitude() != 0) {
                //进行定位数据存储以及围栏告警记录的存储
                deviceInfo = receiverUtils.dealLocation(mqCallNoteMessage.getMqLocationData(), time, deviceInfo, 1);
            }
            //修改设备信息
            deviceInfo.setLastUpTime((new Date()).getTime());
            deviceInfoService.modifyDeviceInfo(deviceInfo);
            //新增一笔通话记录
            CallRecord record = new CallRecord();
            record.setDevice_id(deviceInfo.getDeviceId());
            record.setDevice_imei(deviceInfo.getImei());
            record.setCall_duration(timeResult);
            record.setCall_time(time);
            record.setCall_type(callType);
            record.setPhone(phone);
            logService.addCallRecord(record);
            //指令回复
            irmFeignClientService.replyCallNote(imei, commandNumber);
            //存储设备接收日志
            DeviceLog deviceLog = new DeviceLog();
            deviceLog.setSendUser(Constants.DEVICE_USER);
            deviceLog.setImei(imei);
            deviceLog.setBigType(Constants.CALL_RECORDS);
            JSONObject contant = new JSONObject();
            contant.put("设备号", imei);
            contant.put("来去电号码",mqCallNoteMessage.getPhone());
            contant.put("来去电类型 0-来电 1-去电",callType);
            contant.put("通话时长",timeResult);
            contant.put("日月年",mqCallNoteMessage.getDeviceTime());
            contant.put("经度", mqLocationData.getLongitude());
            contant.put("纬度", mqLocationData.getLatitude());
            deviceLog.setCmd(contant.toJSONString());
            deviceLog.setSendTime(System.currentTimeMillis());
            deviceLog.setCommandNumber(commandNumber);
            deviceLog.setState(1);
            deviceLogService.addDeviceLog(deviceLog);
        }else{
            saveUnInStorage( imei);
            receiverUtils.cloaseChannel(imei);
        }

    }

    /**
     * 通话时长处理
     * @param time
     * @return
     */
    public String dealTime(String time) {
        Integer second = Integer.parseInt(time);
        Integer hour = second / 3600;
        Integer minute = second % 3600 / 60;
        Integer second2 = second % 3600 % 60;
        return convertTime(hour).concat(":").concat(convertTime(minute)).concat(":").concat(convertTime(second2));
    }

    public String convertTime(Integer time) {
        String result = "";
        if (time < 10) {
            result = "0".concat(time.toString());
        } else {
            return time.toString();
        }
        return result;
    }
}
